import * as types from "./mutation-types";
import { playMode } from "common/js/config";
import { shuffle } from "common/js/until";

function findIndex(list, song) {
  return list.findIndex(item => {
    return item.id === song.id;
  });
}
// 点击播放的函数
export const selectPlay = function({ commit, state }, { list, index }) {
  commit(types.SET_PLAY_LIST, list);
  if (state.mode === playMode.random) {
    let randomList = shuffle(list);
    commit(types.SET_PLAY_LIST, randomList);
    index = findIndex(randomList, list[index]);
  } else {
    commit(types.SET_SEQUENCE_LIST, list);
  }

  // 如果不是随机模式，播放索引正常
  commit(types.SET_CURRENT_INDEX, index);
  commit(types.SET_FULL_SCREEN, true);
  commit(types.SET_PLAYING_STATE, true);
};

// 点击按钮随机播放
export const randomPlay = function({ commit }, { list }) {
  //随机播放列表
  let randomList = shuffle(list);
  commit(types.SET_PLAY_MODE, playMode.random);
  commit(types.SET_SEQUENCE_LIST, list);
  commit(types.SET_PLAY_LIST, randomList);
  commit(types.SET_CURRENT_INDEX, 0);
  commit(types.SET_FULL_SCREEN, true);
  commit(types.SET_PLAYING_STATE, true);
};
